﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SEO.Entity;
using System.Data;

namespace SEO.Business
{
    /// <summary>
    /// 新闻源操作
    /// </summary>
    public class BisNewsWeb
    {
        /// <summary>
        /// 获取新闻源
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public NewsWeb Get(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [NewsWeb] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            NewsWeb Info = null;
            while (dr.Read())
            {
                Info = new NewsWeb();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.Name = dr["Name"].ToString();
                Info.Url = dr["Url"].ToString();
                Info.Status = Convert.ToBoolean(dr["Status"]);
                Info.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 新增新闻源
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(NewsWeb Info)
        {
            string strSql = "insert into [NewsWeb]([Name],[Url]) values(@Name,@Url)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Url", Info.Url));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新新闻源
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(NewsWeb Info)
        {
            string strSql = "update [NewsWeb] set [Name]=@Name,[Url]=@Url where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Url", Info.Url));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除新闻源
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [NewsWeb] where [ID]=@ID;",
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 获取新闻源列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(string strName, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[6];
            parms[0] = new SqlParameter("@Name", strName);
            parms[1] = new SqlParameter("@PageIndex", nPageIndex);
            parms[2] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[3] = new SqlParameter("@PageCount", nPageCount);
            parms[3].Direction = ParameterDirection.Output;
            parms[4] = new SqlParameter("@RecordCount", nRecordCount);
            parms[4].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetNewsWebList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[3].Value);
            nRecordCount = Convert.ToInt32(parms[4].Value);
            return dt;
        }

        /// <summary>
        /// 验证新闻源是否存在
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public bool CheckNewsWeb(int nID, string strUrl)
        {
            object retVal = SqlHelper.ExecuteScalar(
                              SqlHelper.database,
                              CommandType.Text,
                              "select top 1 [ID] from [NewsWeb] where [Url]=@Url and (@ID=0 or [ID]<>@ID)",
                              new SqlParameter("@Url", strUrl),
                              new SqlParameter("@ID", nID));
            return retVal == null ? false : true;
        }

        /// <summary>
        /// 验证新闻源是否存在
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public bool CheckNewsWeb(string strUrl)
        {
            object retVal = SqlHelper.ExecuteScalar(
                              SqlHelper.database,
                              CommandType.Text,
                              "select top 1 [ID] from [NewsWeb] where [Url]=@Url",
                              new SqlParameter("@Url", strUrl));
            return retVal == null ? false : true;
        }

        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="nID"></param>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public bool SetStatus(int nID, bool bStatus)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "update [NewsWeb] set [Status]=@Status where [ID]=@ID;",
                    new SqlParameter("@Status", bStatus),
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 获取新闻源ID
        /// </summary>
        /// <param name="strUrl"></param>
        /// <param name="bStatus"></param>
        /// <returns></returns>
        public int GetNewsWebID(string strUrl, bool bStatus)
        {
            object retVal = SqlHelper.ExecuteScalar(
                              SqlHelper.database,
                              CommandType.Text,
                              "select top 1 [ID] from [NewsWeb] where [Url]=@Url and [Status]=@Status",
                              new SqlParameter("@Url", strUrl),
                              new SqlParameter("@Status", bStatus));
            return retVal == null ? 0 : Convert.ToInt32(retVal);
        }
    }
}
